package com.crrcdt.thread;

/**
 * @author lj on 2020/11/18.
 * @version 1.0
 */
public class YieldThreadTest implements Runnable{
    private String name;

    public YieldThreadTest(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i <5 ; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"运行,i ="+i);
            if(i==2){
                System.out.print("线程礼让：") ;
                Thread.yield();//礼让
            }
        }
    }

    public static void main(String[] args) {
        final Thread t1 = new Thread(new YieldThreadTest("线程A"));
        final Thread t2 = new Thread(new YieldThreadTest("线程B"));
        t1.start();
        t2.start();
    }
}
